package com.wx.springboot.step9back.service.impl;

import com.wx.springboot.step9back.common.util.MyDate;
import com.wx.springboot.step9back.common.util.WordUtil;
import com.wx.springboot.step9back.mapper.LogsMapper;
import com.wx.springboot.step9back.model.Logs;
import com.wx.springboot.step9back.service.LogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("logsService")
public class LogsServiceImpl implements LogsService {

    @Autowired
    private LogsMapper logsMapper;

    @Autowired
    private WordUtil wordUtil;

    @Override
    public List<Logs> getMyLogsList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return logsMapper.getMyLogsList(data);
    }

    @Override
    public int getMyLogsCount(Map<String, Object> data) {
        return logsMapper.getMyLogsCount(data);
    }

    @Override
    public List<Logs> getSelectList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return logsMapper.getSelectList(data);
    }

    @Override
    public int getSelectCount(Map<String, Object> data) {
       return logsMapper.getSelectCount(data);
    }

    @Override
    public Logs getLogsById(Integer id) {
        return logsMapper.getLogsById(id);
    }

    @Override
    public String saveLogs(Logs logs) {
        int result = logsMapper.saveLogs(logs);
        if (result >0 ){
            return "success";
        }
        return "false";
    }

    @Override
    public String submitLogs(Logs logs) {
        int result = logsMapper.submitLogs(logs);
        if (result >0 ){
            return "success";
        }
        return "false";
    }

    @Override
    public int checkLogs(Logs logs) {
        logs.setUpdateTime(MyDate.getDate());
        return logsMapper.checkLogs(logs);
    }

    @Override
    public void createWord(Logs logs) {
        wordUtil.createWord(logs);
    }

    @Override
    public int delLogs(Logs logs) {
        logs.setUpdateTime(MyDate.getDate());
        return logsMapper.delLogs(logs);
    }

    @Override
    public String batchDeletion(List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        params.put("updateTime",MyDate.getDate());
        int result = logsMapper.batchDeletion(params);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public void createDeletion(List<Integer> ids) {
        List<Logs> logs = logsMapper.getLogsByIds(ids);
        for (Logs log : logs) {
            wordUtil.createWord(log);
        }
    }

    @Override
    public int saveCheckLogs(Logs logs) {
        logs.setUpdateTime(MyDate.getDate());
        return logsMapper.saveCheckLogs(logs);
    }

    @Override
    public int submitCheckLogs(Logs logs) {
        logs.setUpdateTime(MyDate.getDate());
        return logsMapper.submitCheckLogs(logs);
    }

}
